package homework.day02.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//@Component
@PropertySource("classpath:config.properties")
public class DBUtil implements IDBUtil{

    private String className;
    private String url;
    private String userName;
    private String password;

    public Connection getConnection() throws SQLException {
        System.out.println("正在连接数据库....");
        return DriverManager.getConnection(url,userName,password);
    }

    @Value("${spring.datasource.classname}")
    public void setClassName(String className) {
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Value("${spring.datasource.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    @Value("${spring.datasource.username}")
    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Value("${spring.datasource.password}")
    public void setPassword(String password) {
        this.password = password;
    }
}
